package com.crowntit.domain.trade.service.lock.factory;

import com.crowntit.domain.trade.model.entity.GroupBuyActivityEntity;
import com.crowntit.domain.trade.model.entity.TradeLockRuleBackEntity;
import com.crowntit.domain.trade.model.entity.TradeLockRuleCommandEntity;
import com.crowntit.domain.trade.service.lock.filter.TeamStockOccupyRuleFilter;
import com.crowntit.domain.trade.service.lock.filter.TradeLockActivityUsabilityFilter;
import com.crowntit.domain.trade.service.lock.filter.TradeLockUserTakeLimitFilter;
import com.crowntit.types.design.framework.link.model2.LinkArmory;
import com.crowntit.types.design.framework.link.model2.chain.BusinessLinkedLink;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class TradeLockRuleFilterFactory {

    @Bean("tradeLockRuleFilterLink")
    public BusinessLinkedLink<TradeLockRuleCommandEntity, DynamicContext, TradeLockRuleBackEntity> tradeLockRuleFilterLink(TradeLockActivityUsabilityFilter activityUsabilityFilter, TradeLockUserTakeLimitFilter tradeLockUserTakeLimitFilter, TeamStockOccupyRuleFilter teamStockOccupyRuleFilter){
        LinkArmory<TradeLockRuleCommandEntity, DynamicContext, TradeLockRuleBackEntity> linkArmory = new LinkArmory<>("tradeRuleFilterLink", activityUsabilityFilter, tradeLockUserTakeLimitFilter, teamStockOccupyRuleFilter);
        return linkArmory.getLogicLink();
    }


    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    public static class DynamicContext {
        private GroupBuyActivityEntity groupBuyActivity;
        private Integer userTakeLimitCount;
        private String teamStockKey = "group_buy_market_team_stock_key_";

        public String generateTeamStockKey(String teamId){
            if (StringUtils.isBlank(teamId)) return null;
            return teamStockKey + groupBuyActivity.getActivityId() + "_" + teamId;
        }

        public String generateRecoveryTeamStockKey(String teamId){
            if (StringUtils.isBlank(teamId)) return null;
            return teamStockKey + groupBuyActivity.getActivityId() + "_" + teamId + "_recovery";
        }
    }
}
